package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Category;
import com.beans.Movie;
import com.bussiness.MovieBussinessProxy;
import com.bussiness.PersonBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;
import com.exceptions.MovieAlreadyExistsException;

/**
 * Servlet implementation class AddQueueServlet
 */
public class AddQueueServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PersonBussinessProxy personBI = new PersonBussinessProxy();
	MovieBussinessProxy movieBI = new MovieBussinessProxy();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddQueueServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		RequestDispatcher dispatcher;
		String mname="";
		try
		{
			
			personBI.setEndpoint(ServiceEndPoints.PersonEndPoint);
			movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
			String[] id=request.getParameterValues("movieid");
			long userid = (Long) session.getAttribute("MembershipId");
			for(int j=0;j<id.length;j++){
				int movieid = Integer.parseInt(id[j]);
				System.out.println("movie id"+movieid);
				System.out.println("user id"+userid);
				Movie m = movieBI.getMovieById(movieid);
				mname = m.getMovieName();
				int status = personBI.addtoQueue(movieid,userid);
				System.out.println("status"+status);
			}
			request.setAttribute("status","Added successfully to Queue");	 
			
			dispatcher = getServletContext().getRequestDispatcher("/searchmovie.jsp");
			dispatcher.forward(request, response);
			
		}catch (MovieAlreadyExistsException me){
			request.setAttribute("MovieName", mname);
			request.setAttribute("status", "Movie is already in your queue");
			dispatcher = getServletContext().getRequestDispatcher("/searchmovie.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			request.setAttribute("status", UIMessages.SERVICE_UNAVAILABLE);
			dispatcher = getServletContext().getRequestDispatcher("/searchmovie.jsp");
			dispatcher.forward(request, response);
		}
	}

	}


